package com.luoying.itew.activity;


import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.luoying.itew.bean.DistrictBean;
import com.luoying.itew.bean.MonitorBean;
import com.luoying.itew.bean.PointCityBean;
import com.luoying.itew.http.HttpRequestUtils;
import com.luoying.itew.http.HttpResponseCallBack;
import com.luoying.itew.manage.UserManage;
import com.luoying.itew.util.LogUtils;

import java.util.List;

public class MonitorListViewModel extends ViewModel {
    private MutableLiveData<List<MonitorBean>> liveData = new MutableLiveData<>();
    private MutableLiveData<Boolean> viewStyle = new MutableLiveData<Boolean> ();
    public static ObservableField<String> search = new ObservableField<>();


    public MutableLiveData<List<MonitorBean>> getLiveData() {
        return liveData;
    }

    public MutableLiveData<Boolean>  getViewStyle(){
        return viewStyle;
    }


    public void onRefresh(){
        HttpRequestUtils.getInstance().getUserMapInfo(new HttpResponseCallBack<JsonObject>() {
            @Override
            public void onFail(String message) {
                viewStyle.setValue(false);

            }
            @Override
            public void onSuccess(JsonObject data) {
                Gson gson = new Gson();
                UserManage.getInstance().saveUserCity(gson.fromJson(data.get("userCity"), PointCityBean.class));
                List<DistrictBean> listdDstrict =gson.fromJson(data.get("districtJson"),new TypeToken<List<DistrictBean>>(){}.getType());
                UserManage.getInstance().setMonitorBeanList(listdDstrict);
                List<MonitorBean> list = UserManage.getInstance().getAllMonitorList();
                liveData.setValue(list);
                viewStyle.setValue(false);
            }

            @Override
            public void onError(String message) {
                viewStyle.setValue(false);
            }
        });
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
        LogUtils.e("~~~~"+charSequence);
    }



}
